﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;

namespace SaleSystem.Business
{
    public partial class frm_ClientBackBillList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bnNew.OnClientClick = win_Content.GetShowReference("frm_ClientBackBillEdit.aspx?Id=0", "新增退货单");
                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }

        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.ClientBackBill SM = new Sale_Model.ClientBackBill();

                SM.CBB_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.CBB_Code = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                Manager.GetModel(SM);
                if (SM.CBB_Input == 1)
                {

                    ExtAspNet.Alert.Show("该退货单已经入库，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
                }
                else
                {
                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }

            }
            else if (e.CommandName.ToLower() == "edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_ClientBackBillEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);

            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        protected void gv_List_PreRowDataBound(object sender, ExtAspNet.GridPreRowEventArgs e)
        {
            ExtAspNet.LinkButtonField lbfAction1 = gv_List.FindColumn("edit") as ExtAspNet.LinkButtonField;
            ExtAspNet.LinkButtonField lbfAction2 = gv_List.FindColumn("delete") as ExtAspNet.LinkButtonField;
            DataRowView row = e.DataItem as DataRowView;
            if (ValueHandler.GetIntNumberValue(row["CBB_Input"]) == 1)
            {
                //                lbfAction1.Enabled = false;
                if (ValueHandler.GetIntNumberValue(row["CBB_State"]) == 1)
                {
                    lbfAction1.Text = "查看";
                }
                else
                {
                    lbfAction1.Text = "结算";
                }
                lbfAction2.Enabled = false;

            }
            else
            {
                lbfAction1.Text = "编辑";

                lbfAction1.Enabled = true;
                lbfAction2.Enabled = true;
            }


        }
        #endregion

        #region Method

        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (tb_CBB_Code.Text.Trim() != "")
            {
                strCondition += " CBB_Code LIKE '%" + ValueHandler.GetStringValue(tb_CBB_Code.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (tb_SBill_Code.Text.Trim() != "")
            {
                strCondition += " SBill_Code LIKE '%" + tb_SBill_Code.Text.Trim() + "%' AND ";
            }
            if (tb_SBill_Code2.Text.Trim() != "")
            {
                strCondition += " SBill_Code2 LIKE '%" + tb_SBill_Code2.Text.Trim() + "%' AND ";
            }
            if (tb_Cust_Name.Text.Trim() != "")
            {
                strCondition += " Cust_Name LIKE '%" + tb_Cust_Name.Text.Trim() + "%' AND ";
            }

            if (dp_CBB_Date1.Text.Trim() != "")
            {
                DateTime d1 = DateTime.Now;
                if (DateTime.TryParse(dp_CBB_Date1.Text, out d1))
                {
                    strCondition += " CBB_Date >='" + dp_CBB_Date1.Text + "' AND ";
                }
            }
            if (dp_CBB_Date2.Text.Trim() != "")
            {
                DateTime d2 = DateTime.Now;
                if (DateTime.TryParse(dp_CBB_Date2.Text, out d2))
                {
                    strCondition += " CBB_Date <='" + dp_CBB_Date2.Text + "' AND ";
                }
            }

            if (ddl_CBB_Input.SelectedValue != "-1")
            {
                strCondition += " CBB_Input =" + ddl_CBB_Input.SelectedValue + " AND ";
            }
            if (ddl_CBB_State.SelectedValue != "-1")
            {
                strCondition += " CBB_State =" + ddl_CBB_State.SelectedValue + " AND ";
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        private void BindDropDownList()
        {
            //SysUsersOp userManager = new SysUsersOp();
            //DataTable dtUser = userManager.GetList("");
            //this.ddl_BBill_SUser_Id.DataSource = dtUser;
            //this.ddl_BBill_SUser_Id.DataBind();
            //this.ddl_BBill_SUser_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            //结算状态
            this.ddl_CBB_State.DataSource = LinkService.GetSBill_State();
            this.ddl_CBB_State.DataBind();
            this.ddl_CBB_State.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
        }
        #endregion

        #region Property
        ClientBackBillOp Manager = new ClientBackBillOp();

        #endregion
    }
}
